<?php
/**
 * http://code.google.com/p/php-iterator-utils/
 * @license http://www.apache.org/licenses/LICENSE-2.0.html
 * @author Seva Lapsha (seva.lapsha@gmail.com)
 */
namespace lapsha\iterators\key {
	include_once __DIR__.'/../common.php';
	use \lapsha\common;
	include_once __DIR__.'/common.php';
	use \lapsha\iterators\common\Wrapper;

	/**
	 * Generate Iterator by applying calback to keys of given Iterator
	 */
	class Mapper extends Wrapper {
		protected $callback;
		/**
		 * @param Iterator $i
		 * @param calback $callback
		 */
		function __construct(\Iterator $i, $callback) {
			common\checkCallable($callback);
			parent::__construct($i);
			$this->callback = $callback;
		}
		function key() {
			return call_user_func($this->callback, $this->i->key());
		}
	}
	/**
	 * Generate Iterator of given Iterator's keys
	 * @see array_keys
	 */
	class KeysIterator extends Wrapper {
		protected $key;
		function current() {
			return parent::key();
		}
		function rewind() {
			$this->key = 0;
			return parent::rewind();
		}
		function next() {
			++$this->key;
			return parent::next();
		}
		function key() {
			return $this->key;
		}
	}
	/**
	 * Generate Iterator by changing case of given Iterator's keys
	 * @see array_change_key_case
	 */
	class CaseChanger extends Mapper {
		/**
		 * @param \Iterator $i
		 * @param int $case \CASE_LOWER or \CASE_UPPER
		 */
		function __construct(\Iterator $i, $case) {
			parent::__construct($i, $case == \CASE_LOWER ? 'strToLower' : 'strToUpper');
		}
	}
}